package com.kede.api.auth.config;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.kede.api.auth.utils.WebUtils;
import com.kede.pojo.ResHttp;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/***
 ** @Author:Pr.li
 ** @Description: security 认证过程中出现错误会封装成AuthenticationException 并且会通过这个bean处理
 ** @Date: create in 2023/4/1 15:51
 ***/
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResHttp resHttp = ResHttp.set(HttpStatus.HTTP_UNAUTHORIZED, "认证失败", null);
        String s = JSON.toJSONString(resHttp);
        WebUtils.renderString(response,s);
    }
}
